<?php

namespace app\model;

use support\Model;
use app\model\PayPingModel;

/**
 * 商家模型
 * 
 * 用于管理业务相关数据
 * 包含业务名称、图片、排序等信息
 * 
 * @property int $id 主键ID
 * @property string|null $name 业务名称
 * @property string|null $image 业务图片
 * @property int|null $sort_by 排序值
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property string|null $service 服务项，用@分隔
 * @property string|null $avatar 头像
 * @property int|null $close_time 关门时间
 * @property int|null $opentime 开门时间
 * @property string|null $desc 商家描述
 * @property int|null $multi_biz_id 总店ID
 * @property int|null $multi_biz_total 总店数量
 * @property string|null $master_name 店主姓名
 * @property string|null $master_phone 店主电话
 */
class BizModel extends Model
{
    /**
     * 关联表名
     * @var string
     */
    protected $table = 'biz';

    /**
     * 主键
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 关闭自动时间戳
     * @var bool
     */
    public $timestamps = false;

    /**
     * 可批量赋值字段
     * @var array
     */
    protected $fillable = [
        'name', 'image', 
        'sort_by', 'biz_phone', 
        'shop_address', 'uscc', 
        'service', 'avatar',
        'pos','normal_discount',
        'goods_price_discount','vip_discount',
        'opentime','close_time',
        'desc','multi_biz_id','multi_biz_total',
        'master_name','master_phone'
    ];

    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
    ];

    protected $guarded = [];

    public function safeBizPhone(){
        return substr($this->biz_phone, 0, 3) . '****' . substr($this->biz_phone, -4);
    }

    public function doTags(){
        return empty($this->tag) ? [] : explode('@',$this->tag);
    }

    /**
     * 格式化店铺服务
     * @return string[]
     */
    public function formatService(){
        return empty($this->service) ? [] : explode('@', $this->service);
    }

    

    /**
     * 获取地理位置数组
     * @return mixed
     */
    public function doPos(){
        return empty($this->pos) ? [] : explode(',', $this->pos);
    }

    public function syncStar(){
        $commentTotal = PayPingModel::where('biz_id', $this->id)->count();
        if($commentTotal > 0){
            $star = round(PayPingModel::where('biz_id', $this->id)->avg('star'),1);
            $this->star = $star;
            $this->comment_total = $commentTotal;
            $this->save();
        }else{
            $this->star = 0;
            $this->comment_total = 0;
            $this->save();
        }
    }

    public function formatBusinessHours(): string
    {
        $opentime = $this->opentime;
        $closeTime = $this->close_time;
        if ($opentime === null || $closeTime === null || $opentime === 0 || $closeTime === 0) {
            return '未设置';
        }
        
        $openHour = sprintf('%02d:00', $opentime);
        $closeHour = sprintf('%02d:00', $closeTime);
        
        return $openHour . ' - ' . $closeHour;
    }

    public function normalDiscountLabel(){
        if($this->normal_discount > 0){
            // 将normal_discount转为折扣显示，如95.00显示为9.5折
            return rtrim(rtrim($this->normal_discount / 10, '0'), '.') . '折';
        }else{
            return false;
        }
    }

    public function vipDiscountLabel(){
        if($this->vip_discount > 0){
            return rtrim(rtrim($this->vip_discount / 10, '0'), '.') . '折';
        }else{
            return false;
        }
    }
} 